import router from '@/router/index'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import '@/assets/css/nprogress.less'
import store from '@/store'
import storage from 'store'
import oyNotification from '@/utils/antd/notification'
import {
    getMockMenuList
} from '@/request/api/mock.js'

NProgress.configure({
    showSpinner: false
}) // 进度条配置

router.beforeEach(async (to, from, next) => {
    NProgress.start()

    setTitle(to)

    // 是否有自己的登录页 没有删除登录路由
    if (!judgeIsNeedLoginPage(to)) {
        next(to.fullPath)
    }

    // 是否有token
    if (judgeAndSetToken(to)) {
        // 是否在有登录页的情况下跳转登录页面
        if (to.path === loginPath && store.getters.isLoginPage) {
            next(defaultPath)
        } else {
            let menuList = store.getters.menuList
            // 是否有菜单列表
            if (menuList) {
                next()
            } else {
                menuList = await setMenuData()
                next(to.fullPath)
            }
        }
    } else {
        // 是否在白名单内
        if (allowList.includes(to.name)) {
            next()
        } else {
            // 跳转中台的登录还是自己的登录
            if (goLogin()) {
                next(loginPath)
            } else {
                console.log('跳转中台的登录页逻辑')
                next()
            }
        }
    }
})

router.afterEach(() => {
    NProgress.done() // finish progress bar
})

// 不需要判断token的页面
const loginPath = '/login' // 登录页面路径
const defaultPath = '/' // 主页路径
const allowList = ['Login'] // 页面白名单

// 判断是否需要登录页面
const judgeIsNeedLoginPage = (to) => {
    if (!store.getters.isLoginPage && to.name === 'Login') {
        router.removeRoute('Login')
        return false
    }
    return true
}

// 设置标题
const setTitle = (to) => {
    const title = (to.meta && to.meta.title) || store.getters.systemTitle
    document.title = title
}

// 判断并设置token
const judgeAndSetToken = (to) => {
    let oauth_token = to.query.token
    if (oauth_token) {
        store.commit('user/setToken', oauth_token)
        return true
    }
    oauth_token = store.getters.token || storage.get('token')
    if (oauth_token) {
        store.commit('user/setToken', oauth_token)
        return true
    }
    return false
}

// 跳转登录页
const goLogin = () => {
    if (store.getters.isLoginPage) {
        return true
    } else {
        // 跳转中台的登录页的逻辑
        return false
    }
}

// 获取并设置菜单
const setMenuData = async () => {
    const menuData = await getMockMenuList()
    if (menuData.success) {
        store.commit('app/setMenuList', menuData.data)
        addRouteByMenuList(menuData)
        return menuData
    } else {
        oyNotification({
            type: 'warning',
            description: menuData.message || '请求菜单失败'
        })
        return null
    }
}

// 动态拼接菜单路由
const addRouteByMenuList = () => {
    router.addRoute({
        component: () => import('../layout/menuLayout.vue'),
        path: '/menu',
        children: [{
            name: 'Route1',
            component: () => import('../views/testAddroute/route1.vue'),
            path: '1'
        }, {
            name: 'Route2',
            component: () => import('../views/testAddroute/route2.vue'),
            path: '2'
        },
        {
            name: 'Route3',
            component: () => import('../views/testAddroute/route3.vue'),
            path: '3'
        },
        {
            name: 'Route4',
            component: () => import('../views/testAddroute/route4.vue'),
            path: '4'
        },
        {
            name: 'Detail',
            component: () => import('../views/testAddroute/detail.vue'),
            path: '5/:id'
        }]
    })
}
